//
//  UILabel+RQJJ.m
//  RQJJ
//
//  Created by liyanbin on 19/09/2017.
//  Copyright © 2017 Dc.lee. All rights reserved.
//

#import "UILabel+RQJJ.h"

@implementation UILabel (RQJJ)

/// 根据颜色、字体大小、偏移等初始化UILabel
/// @param textColor 文字颜色
/// @param textFont 文字字体或者大小
/// @param textAlignment 文字偏移
+ (UILabel *)createLableWithTextColor:(UIColor *)textColor andTextFont:(UIFont *)textFont andTextAlignment:(NSTextAlignment)textAlignment {
    UILabel *lable = [[UILabel alloc]init];
    lable.textColor = textColor;
    lable.textAlignment = textAlignment;
    lable.font = textFont;
    
    return lable;
}


/// 向top对齐
- (void)alignTop
{
    CGSize fontSize = [self.text sizeWithAttributes: @{NSFontAttributeName:  self.font}];
    
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    
    
    CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options: NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName:  self.font} context:nil].size;
                            
                            //sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    
    
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    
    for(int i=0; i<= newLinesToPad; i++)
    {
        self.text = [self.text stringByAppendingString:@" \n"];
    }
}

/// 向bottom对齐
- (void)alignBottom
{
    CGSize fontSize = [self.text sizeWithAttributes: @{NSFontAttributeName:  self.font}];
    
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    
    
//    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options: NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName:  self.font} context:nil].size;
    
    
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    
    for(int i=0; i< newLinesToPad; i++)
    {
        self.text = [NSString stringWithFormat:@" \n%@",self.text];
    }
}
@end
